package mbsl.books;

import mbsl.extensions.Extension;
import mbsl.storage.DatabaseTableManipulator;
import mbsl.system.Core;
import mbsl.system.Logger;
import mbsl.system.TextProvider;

public class Books implements Extension {
	public static final TextProvider tp = new TextProvider("mbsl/books/text");
	private static final Logger logger = new Logger(Books.class);
	
	@Override
	public void init(Core pCore) {
		try {
			Class.forName("mbsl.commons.base.ElementPanel");
		} catch (ClassNotFoundException pE) {
			logger.fatal("   Books depends on the mbsl.commons library, and could not find it.");
			
			throw new RuntimeException(pE);
		}
		
		DatabaseTableManipulator manipulator = pCore.getDatabase().getTableManipulator();
		
		if (!manipulator.exists("books") || !manipulator.exists("authors")) {
			logger.info("   Books is generating tables");
			
			manipulator.addScript("mbsl/books/mbsl_books.sql");
			
			manipulator.commit();
		}

		pCore.getDatabase().addResource("mbsl/books/books.hbm.xml");
	}

	@Override
	public void shutdown(Core pCore) {

	}

	@Override
	public void start(Core pCore) {
		new BookController(pCore.getMainController());
	}
	
	public String[] loadAfter() {
		return new String[] {
				"mbsl.commons.Commons" 
		};
	}
}
